package com.taxi.driver.module.main.mine;

import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.config.ConfigRepository;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.MineVO;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class MinePresenter extends BasePresenter implements MineContract.Presenter {

    private final MineContract.View mView;
    private final UserRepository mUserRepository;
    private final ConfigRepository mConfigRepository;


    @Inject
    public MinePresenter(MineContract.View view, UserRepository userRepository, ConfigRepository configRepository) {
        mView = view;
        mUserRepository = userRepository;
        mConfigRepository = configRepository;

    }

    @Override
    public void subscribe() {
        if (mFirstSubscribe) {
            mSubscriptions.add(
                    mUserRepository.getUserInfo()
                            .map(MineVO::createFrom)
                            .compose(RxUtil.applySchedulers())
                            .subscribe(mView::showDriverInfo,
                                    ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository)));
        }
    }

    @Override
    public void unsubscribe() {
        super.unsubscribe();
    }

    @Override
    public int getDriverType() {
        return mUserRepository.getDriverType();
    }

    @Override
    public void getInvite() {
        mConfigRepository.getGovern()
                .compose(RxUtil.applySchedulers())
                .subscribe(governEntity -> {
                    mView.setMessageLogo(governEntity.getMessage());
                    mView.invite(governEntity.getInvite());
                }, throwable -> KLog.e(throwable));
    }

}
